package indi.zhang.rocketmq.listener;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yomahub.tlog.core.mq.TLogMqConsumerProcessor;
import com.yomahub.tlog.core.mq.TLogMqRunner;
import com.yomahub.tlog.core.mq.TLogMqWrapBean;
import indi.zhang.mq.MessageContent;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

@Slf4j
public abstract class TLogListener extends BaseListener {
    @Override
    public void onMessage(Message message) {
        try {
            byte[] body =  message.getBody();
            String s = new String(body, StandardCharsets.UTF_8);
            log.info("待消费的消息内容是：{}",s);
            TLogMqWrapBean<MessageContent> tLogMqWrapBean = JSONObject.parseObject(s, new TypeReference<TLogMqWrapBean<MessageContent>>() {});
            TLogMqConsumerProcessor.process(tLogMqWrapBean, new TLogMqRunner<MessageContent>() {
                @Override
                public void mqConsume(MessageContent o) {
                    //业务操作
                    deal(o);
                }
            });
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

}
